/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.form;

import com.hitechbangla.fctu.base.GameScene;
import com.hitechbangla.fctu.base.aircraft.Aircraft;
import com.hitechbangla.fctu.utility.AircraftUtility;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Asus
 */
public class commandExecutingThread extends Thread implements KeyListener  {
    private GameScene gameScene;
    private boolean commandButtonPressed;
    private Voice voice;
    private String command;
    commandExecutingThread(GameScene gameScene, boolean commandButtonPressed) {
	this.gameScene = gameScene;
	this.commandButtonPressed = commandButtonPressed;
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	 
	 String voiceName = "kevin16";
	VoiceManager voiceManager =  VoiceManager.getInstance();
	voice = voiceManager.getVoice(voiceName);

	if (voice == null) {
	    //System.out.println("Voice not found.");
	    System.exit(1);
	}

	voice.allocate();
	  voice.setRate(120);
	voice.setPitch(100);
	
    }
    
    public void run()
    {
	Scanner in = new Scanner(System.in);
	while(commandButtonPressed)  
        {  
	    voice.speak("Enter commmand");
	    
	    String command = null;
	    command = in.nextLine();
	    voice.speak("You entered "+ command);
//	    try {
//		command = br.readLine();
//		voice.speak("You entered "+ command);
//	    } catch (IOException ex) {
//		Logger.getLogger(AircraftTools.class.getName()).log(Level.SEVERE, null, ex);
//	    }
	    if(AircraftUtility.compareWithCommandRegEx(command))
	    {
		String callSignCommand = command.substring(0, 3); 
//		    jTextAreaCommandLog.setText(callSignCommand +"\n");
//		    //System.out.println(callSignCommand);
		boolean flagAircraftFound=false;
		for(Aircraft aircraft: gameScene.airCraftList)
		{
		    if(aircraft.getAircraftState().getCallSign().equals(callSignCommand))
		    {
			aircraft.executeCommand(command);  
			voice.speak("Executing command "+ command);
			flagAircraftFound = true;
			break;
		    }
		}

		if(flagAircraftFound==false)
		{
		    voice.speak("Aircraft not found, aircraft not found");
		}
	    }
	    else
	    {
		voice.speak("Disregard, Wrong command, Disregard, wrong command");
	    }
	    
        }
	
    }

    @Override
    public void keyTyped(KeyEvent ke) {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyPressed(KeyEvent ke) {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyReleased(KeyEvent e) {
		voice.speak("Inside function");
//	while(commandButtonPressed)  
//        {  
//	    voice.speak("Enter commmand");
//	    
//	    String command = null;
//	    command = in.nextLine();
//	    voice.speak("You entered "+ command);
////	    try {
////		command = br.readLine();
////		voice.speak("You entered "+ command);
////	    } catch (IOException ex) {
////		Logger.getLogger(AircraftTools.class.getName()).log(Level.SEVERE, null, ex);
////	    }
//	    if(AircraftUtility.compareWithCommandRegEx(command))
//	    {
//		String callSignCommand = command.substring(0, 3); 
////		    jTextAreaCommandLog.setText(callSignCommand +"\n");
////		    //System.out.println(callSignCommand);
//		boolean flagAircraftFound=false;
//		for(Aircraft aircraft: gameScene.airCraftList)
//		{
//		    if(aircraft.getAircraftState().getCallSign().equals(callSignCommand))
//		    {
//			aircraft.executeCommand(command);  
//			voice.speak("Executing command "+ command);
//			flagAircraftFound = true;
//			break;
//		    }
//		}
//
//		if(flagAircraftFound==false)
//		{
//		    voice.speak("Aircraft not found, aircraft not found");
//		}
//	    }
//	    else
//	    {
//		voice.speak("Disregard, Wrong command, Disregard, wrong command");
//	    }
//	    
//        }
	
	String keyPressed = String.valueOf(e.getKeyChar());           
            voice.speak(keyPressed);
            if(keyPressed.equals("\n"))
            {   
                if(AircraftUtility.compareWithCommandRegEx(command))
                {
                    String callSignCommand = command.substring(0, 3); 
//		    jTextAreaCommandLog.setText(callSignCommand +"\n");
//		    //System.out.println(callSignCommand);
                    boolean flagAircraftFound=false;
                    for(Aircraft aircraft: gameScene.airCraftList)
                    {
                        if(aircraft.getAircraftState().getCallSign().equals(callSignCommand))
                        {
			    voice.speak("Executing command "+command);
                            aircraft.executeCommand(command);    
                            flagAircraftFound = true;
                        }
                    }
                    
                    if(flagAircraftFound==false)
                    {
			voice.speak("Disregard ,AIRCRAFT NOT FOUND");
                    }
                }
                else
                {
		    voice.speak("Disregard , WRONG COMMAND");
                }
                
                command="";
                
            }
            else
            {
                command += keyPressed;
            }
    }
    
}
